package com.xiaoq;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.xiaoq.matrix.service.impl.ContentPublishReceiver;


@Configuration
@EnableConfigurationProperties({RabbitMQQueueNameProperties.class, RabbitMQConnectionProperties.class})
public class RabbitMQConfig {
	@Autowired
	private RabbitMQConnectionProperties connectionConfig;
	
	@Autowired
	private RabbitMQQueueNameProperties queueNameConf;
	
	@Autowired
	private ConnectionFactory connectionFactory;

	@Autowired
	private SMSDeliverReceiver smsDeliver;
	
	@Autowired
	private SMSDeliverReceiver2 smsDeliver2;

	@Autowired
	private MailDeliverReceiver mailDeliver;
	
	@Autowired
	private ContentPublishReceiver contentPubReceiver;
	
	/**
	 * 用来连接MQ的
	 * @return
	 */
	@Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(connectionConfig.getHost(), connectionConfig.getPort());
        connectionFactory.setUsername(connectionConfig.getUserName());
        connectionFactory.setPassword(connectionConfig.getPassword());
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPublisherConfirms(true);
        return connectionFactory;
    }

	/**
	 * template 用来发消息的
	 * @return
	 */
	@Bean("templateA")
	public RabbitTemplate jmsTemplateReceive() {
		RabbitTemplate tpl = new RabbitTemplate();
		tpl.setConnectionFactory(connectionFactory);
		return tpl;
	}
	
	//@Bean("templateB")
	public RabbitTemplate jmsTemplateQueueSend() {
		RabbitTemplate tpl = new RabbitTemplate();
		tpl.setConnectionFactory(connectionFactory);
		return tpl;
	}
	
	//@Bean("templateC")
	public RabbitTemplate jmsTemplateTopicSend() {
		RabbitTemplate tpl = new RabbitTemplate();
		tpl.setConnectionFactory(connectionFactory);
		return tpl;
	}
	
	@Bean
	public Queue smsQueue() {
		return new Queue(queueNameConf.getSms(), true);
	}
	
	@Bean
	public Queue mailQueue() {
		return new Queue(queueNameConf.getMail(), true);
	}
	
	/**
	 * 这些都是创建一些消息队列
	 * @return
	 */
	@Bean
	public Queue contentPubQueue() {
		return new Queue("matrix.content.pub", true);
	}
	
	@Bean
	public TopicExchange topicExchange() {
		return new TopicExchange("xiaoq-matrix-exchange");
	}
	
	@Bean
	public FanoutExchange fanoutExchange() {
		return new FanoutExchange("xiaoq-matrix-exchange-fanout");
	}
	
	@Bean
    public SimpleRabbitListenerContainerFactory myFactory(
    		SimpleRabbitListenerContainerFactoryConfigurer configurer) {
		SimpleRabbitListenerContainerFactory factory =
                new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
	}

	/**
	 * 把Queue和exchange进行绑定
	 * @return
	 */
	@Bean
	public Binding smsBinding() {
		return BindingBuilder.bind(smsQueue()).to(topicExchange()).with(smsQueue().getName());
	}
	
	@Bean 
	Binding mailBinding() {
		return BindingBuilder.bind(mailQueue()).to(topicExchange()).with(mailQueue().getName());
	}

	@Bean 
	Binding contentPubBinding() {
		Queue contentQueue = contentPubQueue();
		return BindingBuilder.bind(contentQueue).to(topicExchange()).with(contentQueue.getName());
	}
	
	
	@Bean("smsListenerContainer")
	public SimpleMessageListenerContainer smsContainer() {
		return build(smsQueue(), smsDeliver);
	}
	
	// 一个队列两个消费者，随机分发
	@Bean("smsListenerContainer2")
	public SimpleMessageListenerContainer smsContainer2() {
		return build(smsQueue(), smsDeliver2);
	}

	@Bean("mailListenerContainer")
	public SimpleMessageListenerContainer mailContainer() {
		return build(mailQueue(), mailDeliver);
	}
	
	/**
	 * 把监听到的信息交给谁处理
	 * @return
	 */
	@Bean("contentPubContainer")
	public SimpleMessageListenerContainer contentPubContainer() {
		return build(contentPubQueue(), contentPubReceiver);
	}
	
	private SimpleMessageListenerContainer build(Queue queue, AbstractReceiver recv) {
		SimpleMessageListenerContainer lc = new SimpleMessageListenerContainer();
		lc.setConnectionFactory(connectionFactory);
		lc.setQueues(queue);
		MessageListenerAdapter la = new MessageListenerAdapter(recv);
		lc.setMessageListener(la);
		lc.setConcurrentConsumers(1);
		return lc;
	}

	
}
